import { fileURLToPath, URL } from 'node:url'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
// import vueJsx from '@vitejs/plugin-vue-jsx'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import legacy from '@vitejs/plugin-legacy'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import path from 'path'
import { fpiELementPlusResolver } from '@ued_fpi/element-plus-expand'
import Inspect from 'vite-plugin-inspect'
import { name } from './package.json'

// https://vitejs.dev/config/
export default defineConfig(({ mode, command }) => {
    return {
        base: command === 'serve' ? '/' : mode === 'preview-build' ? '/' : `/${name}/`,
        plugins: [
            vue(),
            // vueJsx(),
            // 自动导入
            AutoImport({
                resolvers: [ElementPlusResolver()],
                imports: ['vue', 'vue-router', 'pinia'],
                dts: 'auto-imports.d.ts',
            }),
            // 自动导入组件
            Components({
                dirs: [],
                resolvers: [ElementPlusResolver(), fpiELementPlusResolver()],
            }),
            // 兼容性工具
            legacy({
                targets: ['defaults', 'chrome 62'], //需要兼容的目标列表，可以设置多个
                additionalLegacyPolyfills: ['regenerator-runtime/runtime'],
                renderLegacyChunks: true,
                polyfills: [
                    'es.symbol',
                    'es.array.filter',
                    'es.promise',
                    'es.promise.finally',
                    'es/map',
                    'es/set',
                    'es.array.for-each',
                    'es.object.define-properties',
                    'es.object.define-property',
                    'es.object.get-own-property-descriptor',
                    'es.object.get-own-property-descriptors',
                    'es.object.keys',
                    'es.object.to-string',
                    'web.dom-collections.for-each',
                    'esnext.global-this',
                    'esnext.string.match-all',
                ],
            }),
            // 将svg文件处理为可使用的标签
            createSvgIconsPlugin({
                // 指定要缓存的图标文件夹
                iconDirs: [path.resolve(process.cwd(), 'src/components/SvgIcon/icons')],
                // 执行icon name的格式
                symbolId: 'icon-[dir]-[name]',
            }),
            Inspect(),
        ],
        resolve: {
            alias: {
                '@': fileURLToPath(new URL('./src', import.meta.url)),
            },
        },
        server: {
            strictPort: true,
            port: 10002,
            open: true,
            proxy: {
                '^/api': {
                    target: 'http://aims-test.fpi-inc.site',
                    rewrite: path => path.replace(/^\/api/, ''),
                    changeOrigin: true,
                },
            },
        },
        build: {
            rollupOptions: {
                output: {
                    // 解决打包时Some chunks are larger警告
                    manualChunks(id) {
                        if (id.includes('node_modules')) {
                            return id.toString().split('node_modules/')[1].split('/')[0].toString()
                        }
                    },
                },
            },
        },
    }
})
